import { IVMUtil } from "../Interfaces/IMVVM";

/**VM 工具类 */
export class VMUtil implements IVMUtil {
  private static _instance: VMUtil;
  public static get instance(): VMUtil {
    if (this._instance == null) {
      this._instance = new VMUtil();
    }
    return this._instance;
  }
  /**销毁函数名称 */
  destroyName: string = "destroy";
  /**
   * 定义值监听函数
   */
  Define(obj, key, val, enumerable?) {
    Object.defineProperty(obj, key, {
      value: val,
      enumerable: !!enumerable,
      configurable: true,
      writable: true,
    });
  }
  /**
   *  是否包含指定键值
   */
  hasOwn(obj, key) {
    return window.hasOwnProperty.call(obj, key);
  }

  /**
   * 移除数据
   */
  delData(target, key) {
    if (Array.isArray(target) && typeof key === "number") {
      target.splice(key, 1);
      return;
    }
    let ob = target.__ob__;
    if (!this.hasOwn(target, key)) {
      return;
    }
    delete target[key];
    if (!ob) {
      return;
    }
    ob.dep.notify();
  }
}
